package com.mylove.logger

import android.Manifest
import android.annotation.SuppressLint
import android.app.Application
import android.content.pm.ApplicationInfo
import android.os.Bundle
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import com.benyanyi.loglib.Jlog
import com.benyanyi.loglib.LogConfig
import com.benyanyi.permissionlib.PermissionHelper
import com.benyanyi.permissionlib.callback.PermissionCallBack

/**
 * @author myLove
 * @time 2017/11/16 19:18
 * @e-mail mylove.520.y@gmail.com
 * @overview
 */
class MainActivity : AppCompatActivity() {
    private var tv: TextView? = null
    public override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        tv = findViewById(R.id.text)
        //        tv.setText(isApkInDebug() + "");
//        Jlog.v("测试" + isApkInDebug());
//        Jlog.d("测试")
        PermissionHelper.with(this)
            .setPermissions(
                Manifest.permission.WRITE_EXTERNAL_STORAGE,
                Manifest.permission.READ_EXTERNAL_STORAGE
            ).request(object : PermissionCallBack() {
                override fun onSuccess() {
//                    Jlog.d()
                    Jlog.json("{\"name\":\"BeJson\",\"url\":\"http://www.bejson.com\",\"page\":88,\"isNonProfit\":true,\"address\":{\"street\":\"科技园路.\",\"city\":\"江苏苏州\",\"country\":\"中国\"},\"links\":[{\"name\":\"Google\",\"url\":\"http://www.google.com\"},{\"name\":\"Baidu\",\"url\":\"http://www.baidu.com\"},{\"name\":\"SoSo\",\"url\":\"http://www.SoSo.com\"}]}")
//                    Jlog.v("啊哈111")
                }
            })
//        PermissionHelper.getInstance(this).hasPermission(0x10, object : PermissionCallBack {
//            override fun onPermissionSuccess(i: Int) {
//                Jlog.writeFile("啊哈")
//                Jlog.errorWriteFile(NullPointerException("异常"))
//            }
//
//            override fun onPermissionFailure(failureMsg: FailureMsg) {}
//            override fun onPermissionComplete(i: Int) {}
//        }, Manifest.permission.WRITE_EXTERNAL_STORAGE)
    }

    private val isApkInDebug: Boolean
        get() = try {
            @SuppressLint("PrivateApi") val application =
                Class.forName("android.app.ActivityThread").getMethod("currentApplication")
                    .invoke(null) as Application
            val info = application.applicationInfo
            info.flags and ApplicationInfo.FLAG_DEBUGGABLE == 0
        } catch (e: Exception) {
            Jlog.e(e.message)
            true
        }
}